package com.yj.springbootstudy.mulThread.imooc.juctools;

import java.util.Random;
import java.util.concurrent.Exchanger;

public class ExchangeTest {

    private static Exchanger<String> exchanger = new Exchanger<String>();

    public static void main(String[] args) {

        new Thread(()->{
            System.out.println("快递员开始配送快递");
            try {
                Thread.sleep(new Random().nextInt(5000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("已经到达顾客家门口，等待顾客取件！");
            String s = null;
            try {
                s = exchanger.exchange("iphone13 pro");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("收到付款："+s);

        },"快递员").start();

        new Thread(()->{
            System.out.println("顾客在家等待快递件到达");
            try {
                Thread.sleep(new Random().nextInt(5000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("准备开门取件！");
            String s = null;
            try {
                s = exchanger.exchange("10000￥");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("收到物品："+s);

        },"顾客").start();

    }
}
